package com.college.heyuhua.controller;

import com.college.heyuhua.entity.Newsinfo;
import com.college.heyuhua.model.BBSInfo;
import com.college.heyuhua.service.IBBSInfoListForPage;
import com.college.heyuhua.service.INewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by 赵东坡 on 2018/12/31.
 * Time:13:51
 * 宇宙无敌赵牛逼，祈求永无bug
 */
@Controller
@Slf4j
public class NewController {

    @Autowired
    private INewService iNewService;

    @Autowired
    private IBBSInfoListForPage ibbsInfoListForPage;

    /*根据nid获取数据，从主页过来的连接*/
    @RequestMapping("/getNewsMessage/nid/{nid}")
    public String getNewsMessage(@PathVariable("nid") String nid,ModelMap map){
        if(StringUtils.isEmpty(nid)) {
            return "redirect";
        }
        Newsinfo newsinfo = iNewService.getOneMessage(Long.parseLong(nid));
        //TODO redis记录CTR

        List<Newsinfo> newsinfoList = new ArrayList<>();
        newsinfoList.add(newsinfo);
        map.put("newslist",newsinfoList);
        map.put("showcontent","1");

        //走第一个表,获取新闻评论
        List<BBSInfo> bbsInfoList = ibbsInfoListForPage.getBBSInfoList(nid,"newsbbs");
        map.put("bbsInfoList",bbsInfoList);

        return "indexhome";
    }


    @RequestMapping("/indexhome")
    public String homepagefour(ModelMap map) {
        List<Newsinfo> list =  iNewService.homepagefour();
        if(list.size() > 0 && list != null) {
            map.put("newslist", list);
            return "indexhome";
        }
        else{
            return "404.ftl";
        }
    }

    @RequestMapping("/")
    public void beforecheck() {
        //TODO 获取redis登录信息
        Newsinfo newsinfo = iNewService.getOneMessage(Long.parseLong("0"));
    }


    @RequestMapping("/getMessage/newsstyleid/{newsstyleid}")
    public String getnewstyleid(@PathVariable("newsstyleid") String newsstyleid,ModelMap map) {
        int tempid = 0;
        if(StringUtils.isEmpty(newsstyleid)){
            return "404.ftl";
        }else{
            //TODO redis记录CTR

            tempid = Integer.parseInt(newsstyleid);
        }
        List<Newsinfo> list =  iNewService.getNewsBynewsstyleid(tempid);
        if(list.size() > 0 && list != null) {
            map.put("newslist", list);
            return "indexhome";
        }
        else{
            return "404.ftl";
        }
    }

    @RequestMapping("/inputContent")
    public void inputContent(@RequestParam("bbscontent") String bbscontent,
                             @RequestParam("nid") String nid, HttpServletResponse response) {

        String style = "newsbbs";
        long uid = 0l;
        boolean flag = ibbsInfoListForPage.insertContent(Long.parseLong(nid),bbscontent,style,uid);

        response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        PrintWriter writer = null;
        try{
            writer = response.getWriter();
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
        if(flag == false) {
            writer.print("修改失败");
            writer.close();
            writer.flush();
        }
        writer.print("修改成功");
        writer.close();
        writer.flush();
        log.info(flag?"【插入评论成功】":"【插入评论失败】");
    }


    /*@RequestMapping("/showBBSforNews/nid/{nid}")
    public String showBBSforNews(@PathVariable("nid") String nid) {

        if(StringUtils.isEmpty(nid)) {
            return "404.ftl";
        }else{
            //新闻评论走第一表
            List<BBSInfo> bbsInfoList = ibbsInfoListForPage.getBBSInfoList();
        }
        return null;
    }*/
}
